package com.biz.solr.biz.conifg;

import com.biz.solr.entity.IDoc;
import org.apache.solr.client.solrj.beans.Field;

import java.util.ArrayList;

/**
 * @ClasName BIZConfigHelper
 * @Descripution TODO
 * @Author USER
 * @Date 2022/3/26 9:12
 * @Version V1.0
 */
public class BIZConfig {

    private static ArrayList<BIZConfig> bizConfigs = new ArrayList<>();

    private String bizType;
    private String bizCore;
    private String dataImportUrl;
    private String dataUpdateUrl;
    private Object DocClass;

    public BIZConfig(String bizType, String bizCore, Class docClass) {
        this.bizType = bizType;
        this.bizCore = bizCore;
        this.DocClass = IDoc.getConfigInstance(docClass);
        this.dataImportUrl = "/"+bizCore+"/dataimport?command=full-import&clean=false&commit=true&entity=" + bizCore;
        this.dataUpdateUrl = "/"+bizCore+"/dataimport?command=delta-import&clean=false&commit=true&entity=" + bizCore;
    }

    public static void addConfig(BIZConfig bizConfig) {
        bizConfigs.add(bizConfig);
    }

    public String getBizType() {
        return bizType;
    }

    public String getBizCore() {
        return bizCore;
    }

    public String getDataImportUrl() {
        return dataImportUrl;
    }

    public String getDataUpdateUrl() {
        return dataUpdateUrl;
    }

    public Object getDocClass() {
        return DocClass;
    }


    /**
     * 获取biz
     * @param bizCore
     * @return
     */
    public static BIZConfig getSearchBizByCore(String bizCore){
        for (BIZConfig searchBIZ : BIZConfig.values()) {
            if(searchBIZ.getBizCore().equals(bizCore)){
                return searchBIZ;
            }
        }
        return null;
    }

    public static BIZConfig[] values() {
        return bizConfigs.toArray(new BIZConfig[]{});
    }

    /**
     * 获取biz
     * @param bizType
     * @return
     */
    public static BIZConfig getSearchBizByBiz(String bizType){
        for (BIZConfig searchBIZ : BIZConfig.values()) {
            if(searchBIZ.getBizType().equals(bizType)){
                return searchBIZ;
            }
        }
        return null;
    }

    /**
     * 获取当前 BIZConfig中DocClass的SolrSearch字段名
     * @param filedName
     * @return
     */
    public String getFiledName(String filedName){
        Class docClass = this.getDocClass().getClass();
        try {
            java.lang.reflect.Field field = docClass.getDeclaredField(filedName);
            Field[] annotationsByType = field.getAnnotationsByType(Field.class);
            java.lang.String value = annotationsByType[0].value();
            return value;
        } catch (NoSuchFieldException e) {
            return null;
        }
    }
}
